package cn.hwua.dao.impl;

import cn.hwua.dao.NewsDao;
import cn.hwua.entity.Company;
import cn.hwua.entity.News;
import cn.hwua.util.DBUtil;
import cn.hwua.util.StringUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class NewsDaoImpl implements NewsDao {
    Connection conn=DBUtil.getConnection();
    QueryRunner qr=new QueryRunner();
    @Override
    public int save(News obj) {
        String sql="INSERT INTO  tb_news(news_id,news_title,news_content,news_addtime,news_readnums,news_author,news_img) VALUES (?,?,?,?,?,?,?)";
        Object[] paras={obj.getNews_id(),obj.getNews_title(),obj.getNews_content(),obj.getNews_addtime(),obj.getNews_readnums(),
                obj.getNews_author(),obj.getNews_img()};
        try {
            int row = qr.update(conn, sql, paras);
            return row;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int update(News obj) {
        String sql="UPDATE  tb_news SET  news_title=?,news_content=?,news_addtime=?,news_readnums=?,news_author=?,news_img=? WHERE news_id=? ";
        Object[] paras={obj.getNews_title(),obj.getNews_content(),obj.getNews_addtime(),obj.getNews_readnums(),
                obj.getNews_author(),obj.getNews_img(),obj.getNews_id()};
        try {
            int row = qr.update(conn, sql, paras);
            return row;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int delete(News obj) {
        String sql="DELETE FROM tb_news WHERE news_id  = ? ";
        Object[] paras={obj.getNews_id()};
        try {
            int row = qr.update(conn, sql, paras);
            return row;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public News findById(Object id) {
        String sql="SELECT * FROM tb_news where news_id  = ?";
        Object[] paras={id};
        try {
            News news=qr.query(conn,sql,paras,new BeanHandler<News>(News.class));
            return news;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<News> findAll() {
        String sql="SELECT * FROM tb_news";
        Object[] paras={};
        try {
            List<News> newsList=qr.query(conn,sql,paras,new BeanListHandler<News>(News.class));
            return newsList;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<News> findSearch(String keyword) {
        String sql="SELECT * FROM tb_news where news_title like concat('%',concat(?,'%'))";
        Object[] paras={keyword};
        try {
            List<News> newsList=qr.query(conn,sql,paras,new BeanListHandler<News>(News.class));
            return newsList;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<News> findPage(int page, int pagesize) {
        String sql="SELECT * FROM tb_news limit ?,?";
        Object[] paras={(page-1)*pagesize,pagesize};
        try {
            List<News> newsList=qr.query(conn,sql,paras,new BeanListHandler<News>(News.class));
            return newsList;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public long count() {
        String sql="select count(*) from tb_news";
        Object[] paras={};
        try {
            long nums=qr.query(conn,sql,paras,new ScalarHandler<Long>());
            return nums;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int increaseReadNums(int id) {
        String sql="update tb_news set news_readnums=news_readnums+1 WHERE news_id=?";
        Object[] paras={id};
        try {
            int row = qr.update(conn, sql, paras);
            return row;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<News> reading() {
        String sql="SELECT * FROM tb_news  ORDER BY news_readnums DESC limit 0,8";
        Object[] paras={};
        try {
            List<News> newsList=qr.query(conn,sql,paras,new BeanListHandler<News>(News.class));
            return newsList;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
